package com.itheima02.map;

import java.util.HashMap;
import java.util.Objects;

public class Demo2_Map {
    public static void main(String[] args) {
        Goods g1 = new Goods("好吃饼干",10);
        Goods g2 = new Goods("好吃饼干",10);
        Goods g3 = new Goods("好吃糖果",20);

        //把商品存入到数组中
        Goods[] gArr = new Goods[3];
        gArr[0] = g1;
        gArr[1] = g2;
        gArr[2] = g3;

        //统计商品的出现次数
        HashMap<Goods, Integer> hm = new HashMap<>();
        //对数组进行遍历
        for (Goods goods : gArr) {
            if (!hm.containsKey(goods)) {
                //如果商品在集合中不包含，说明是第一次出现
                hm.put(goods, 1);
            } else {
                //说明商品在集合中不是第一次出现
                hm.put(goods,hm.get(goods)+1);
            }
        }
        System.out.println(hm);
    }
}

class Goods {
    private String brand;
    private int price;

    public Goods() {
    }

    public Goods(String brand, int price) {
        this.brand = brand;
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Goods goods = (Goods) o;
        return price == goods.price &&
                Objects.equals(brand, goods.brand);
    }

    @Override
    public int hashCode() {
        return Objects.hash(brand, price);
    }

    @Override
    public String toString() {
        return "Goods{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}
